TARGET = ucosii4skyeye
UCOS_DIR=./ucos
LWIP_DIR = ./lwip

CROSS_COMPILE ?= arm-elf-
CC = $(CROSS_COMPILE)gcc
LD = $(CROSS_COMPILE)ld
OBJDUMP = $(CROSS_COMPILE)objdump
OBJCOPY = $(CROSS_COMPILE)objcopy

CFLAGS = -O2 -mlittle-endian -march=armv4 -mtune=arm7tdmi -Wall -g -Wstrict-prototypes -Wno-trigraphs -pipe -mshort-load-bytes -mapcs-32
LDFLAGS = -Tlink.lds  -p -X
INCLUDE = -I. -I$(UCOS_DIR) -I$(LWIP_DIR)/include -I$(LWIP_DIR)/arch/ucosII/include -I$(LWIP_DIR)/include/ipv4

CFILES = $(wildcard *.c)
SFILES = $(wildcard *.S)

UCOS_SFILES = $(wildcard $(UCOS_DIR)/*.S)
UCOS_CFILES = $(wildcard $(UCOS_DIR)/*.c) 
UCOS_FILES = $(UCOS_SFILES) $(UCOS_CFILES)
UCOS_OBJS = $(notdir $(UCOS_SFILES:.S=.o)) $(notdir $(UCOS_CFILES:.c=.o)) 

LWIP_API_FILES = $(wildcard $(LWIP_DIR)/api/*.c)
LWIP_CORE_FILES = $(wildcard $(LWIP_DIR)/core/*.c) \
				  $(wildcard $(LWIP_DIR)/core/ipv4/*.c)
LWIP_NETIF_FILES = $(wildcard $(LWIP_DIR)/netif/*.c)
LWIP_ARCH_FILES = $(wildcard $(LWIP_DIR)/arch/ucosII/*.c) \
				  $(wildcard $(LWIP_DIR)/arch/ucosII/netif/*.c)

LWIP_FILES = $(LWIP_API_FILES) $(LWIP_CORE_FILES) $(LWIP_NETIF_FILES) $(LWIP_ARCH_FILES)
LWIP_OBJS = $(notdir $(LWIP_API_FILES:.c=.o)) \
	        $(notdir $(LWIP_CORE_FILES:.c=.o)) \
			$(notdir $(LWIP_NETIF_FILES:.c=.o)) \
			$(notdir $(LWIP_ARCH_FILES:.c=.o))

OBJ_FILES = $(SFILES) $(CFILES) $(UCOS_FILES) $(LWIP_FILES)
OBJ = $(SFILES:.S=.o) $(CFILES:.c=.o) $(UCOS_OBJS) $(LWIP_OBJS)


#ifneq (.depend,$(wildcard .depend))
#depend:dep
#endif

all : $(TARGET).bin
	@echo "done"

ifeq (.depend, $(wildcard .depend))
include .depend
endif

%.o :
	@echo "complie file:(" $< ")" #$@
	$(CC) -c $(CFLAGS) $(INCLUDE) $(<:.o=.c)
	@echo


dep : $(OBJ_FILES)
	$(CC) $(CFLAGS) $(INCLUDE) -MM $^ > .depend || rm -f .depend


$(TARGET).bin : $(TARGET).elf
	$(OBJCOPY) -O binary --remove-section=.ramvec \
		--remove-section=.bss --remove-section=.data \
		--remove-section=.eram \
		--set-section-flags=.romvec=CONTENTS,ALLOC,LOAD,READONLY,CODE \
		$< $@

$(TARGET).elf : $(OBJ)
	$(LD) $(LDFLAGS) $(OBJ) -lgcc ./libuc.a -o $@
#	$(LD) $(LDFLAGS) $^ -o $@ 
#	arm-elf-objdump -S boot_code.elf > boot_code.s
#	arm-elf-readelf -a boot_code.elf > boot_code.r
#	arm-elf-nm boot_code.elf > boot_code.n


#bootloader.o: bootloader.S
#	$(CC) $(CFLAGS) -c $< -o $@
#
#main.o : main.c
#	$(CC) $(CFLAGS) -c $<

clean:
	rm -f *.o $(TARGET).* .depend

test : all
	sudo ~/skyeye-0.9.2 $(TARGET).elf

debug : all
	skyeye -e $(TARGET).elf -d

dump : all
	$(CROSS_COMPILE)objdump $(TARGET).elf -d | less
